void balanced_sorting(int x[], int n)
{
    for (int i = n - 1; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (x[j] > x[j + 1])
            {
                int t = x[j];
                x[j] = x[j + 1];
                x[j + 1] = t;
            }
        }
    }
    int left = 0, right = n - 1, flag = 1;
    int p = 0;
    int a[100];
    while (left <= right)
    {
        if (flag == 1)
            a[left++] = x[p++];
        else
            a[right--] = x[p++];
        flag = -flag;
    }
    for (int i = 0; i < n; i++)
        x[i] = a[i];
}